package com.cy.pj.brand.controller;

import com.cy.pj.brand.pojo.Brand;
import com.cy.pj.brand.service.BrandService;
import org.apache.ibatis.annotations.Mapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/brand")
public class BrandController {

    @Autowired
    private BrandService brandService;
    @RequestMapping({"/findBrandByName/{name}","/findBrandByName"})
    public String findBrandByName(@PathVariable(required = false) String name, Model model){
        List<Brand> brandList=brandService.findBrand(name);
        model.addAttribute("brandList",brandList);
        return "brand";
    }
    @RequestMapping("/deleteBrandById/{id}")
    public String deleteBrandById(@PathVariable Integer id,Model model){
        Integer rows=brandService.deleteById(id);
        List<Brand> brandList=brandService.findBrand(null);
        model.addAttribute("brandList",brandList);
        return "brand";
    }
    /*添加*/
   @RequestMapping("/addBrands")
    public String addBrands(Brand brand,Model model){
        brandService.addBrand(brand);
         List<Brand> brandList=brandService.findBrand(null);
         model.addAttribute("brandList",brandList);
        return "brand";
    }
    /*跳转到添加页面*/
    @RequestMapping("/addUI")
    public String addUI(){
       return "brand_add";
    }

    /*修改*/
    @RequestMapping("/doFinfById/{id}")
    public String doFinfById(@PathVariable  Integer id,Model model){

        Brand brand=brandService.findById(id);
        model.addAttribute("brands", brand);
        return "update_brand";

    }
    @RequestMapping("/doUpdate")
    public String doUpdate(Brand                                                                                                                                    brand,Model model){
        brandService.updateBrand(brand);
        List<Brand> brandList=brandService.findBrand(null);
        model.addAttribute("brandList",brandList);
        return "brand";
    }

}
